package org.lds.scheduled.job.admin.business.worker.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.lds.scheduled.job.admin.business.app.entity.App;
import org.lds.scheduled.job.admin.business.app.mapper.AppMapper;
import org.lds.scheduled.job.admin.business.worker.entity.Worker;
import org.lds.scheduled.job.admin.business.worker.mapper.WorkerMapper;
import org.lds.scheduled.job.admin.business.worker.model.request.WorkerBaseCreateRequest;
import org.lds.scheduled.job.admin.business.worker.model.request.WorkerBaseGetByKeyRequest;
import org.lds.scheduled.job.admin.business.worker.model.request.WorkerBaseUpdateRequest;
import org.lds.scheduled.job.admin.business.worker.model.response.WorkerBaseResponse;
import org.lds.scheduled.job.admin.business.worker.service.WorkerBaseService;
import org.lds.scheduled.job.admin.business.worker.service.WorkerService;
import org.lds.scheduled.job.admin.client.core.model.request.RegisterWorkerRequest;
import org.lds.scheduled.job.admin.common.enums.AdminErrorEnum;
import org.lds.scheduled.job.common.model.dto.ResultDTO;
import org.lds.scheduled.job.common.util.AssertUtil;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 服务实现类
 *
 * @author lidongsheng
 * @since 2025-01-16
 */
@RequiredArgsConstructor
@Service
public class WorkerServiceImpl extends ServiceImpl<WorkerMapper, Worker> implements WorkerService {
    private final AppMapper appMapper;
    private final WorkerBaseService workerBaseService;

    @Override
    public ResultDTO<Long> registerWorker(RegisterWorkerRequest request) {
        Long appId = getAppId(request.getAppName());

        Worker worker = workerBaseService.getWorkerByKey(
                WorkerBaseGetByKeyRequest.builder()
                        .appId(appId)
                        .ip(request.getIp())
                        .port(request.getPort())
                        .build());
        if (Objects.nonNull(worker)) {
            if (BooleanUtils.isFalse(worker.getEnable())) {
                workerBaseService.updateWorker(
                        WorkerBaseUpdateRequest.builder()
                                .id(worker.getId())
                                .enable(true)
                                .build());
            }
            return ResultDTO.success(worker.getId());
        } else {
            ResultDTO<WorkerBaseResponse> resultDTO = workerBaseService.createWorker(
                    WorkerBaseCreateRequest.builder()
                            .appId(appId)
                            .ip(request.getIp())
                            .port(request.getPort())
                            .build());
            return ResultDTO.success(resultDTO.getData().getId());
        }
    }

    private Long getAppId(String appName) {
        if (StringUtils.isEmpty(appName)) return 0L;
        App app = appMapper.selectOne(new QueryWrapper<>(App.builder().name(appName).build()));
        AssertUtil.assertTrue(Objects.nonNull(app), AdminErrorEnum.APP_NOT_EXIST);
        return app.getId();
    }

}
